package com.bestdata.forward.datareceive;


import com.alibaba.fastjson.JSONObject;
import com.bestdata.forward.cachecenter.CacheCenterFactory;
import com.bestdata.forward.model.BaseParamDTO;
import com.bestdata.forward.model.DeviceDTO;
import com.netty.im.client.core.EMDataConnection;
import com.netty.im.client.core.EMDataReceive;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

/**
 * @author chizy
 * @title:
 * @description: 接收PMS云转发的数据
 * @date 2021年1月17日11:55:10
 */

@Service
public class DataReceiveProcess extends Thread implements EMDataReceive {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private Thread thread;

    @Value("${clientkey}")
    private String clientKey;

    @Value("${ip}")
    private String ip;

    @Value("${port}")
    private String port;

    @PostConstruct
    public void init() {
        this.thread = new Thread(this);
        this.thread.start();
    }

    @Override
    public void run() {
        EMDataConnection emDataUtil = new  EMDataConnection();

        emDataUtil.start(clientKey,ip , Integer.parseInt(port), this);
        logger.info("Client Key:" + clientKey + "," + ip + "," + port);
    }

    @Override
    public void ConsumptionData(String msg) {
        if (msg == null){
            logger.error("msg is null");
            return;
        }
        //logger.error("###recv:" + msg);

        try {
            DeviceDTO sensorDataDto = JSONObject.parseObject(msg, DeviceDTO.class);

            CacheCenterFactory.queue.put(sensorDataDto);

        } catch (Exception e){
            e.printStackTrace();
        }

    }
}

